package com.briup.smartcity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.briup.smartcity.bean.BaseConfig;
import com.briup.smartcity.exception.ServiceException;
import com.briup.smartcity.mapper.BaseConfigMapper;
import com.briup.smartcity.mapper.extend.BaseConfigExtendMapper;
import com.briup.smartcity.service.IBaseConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseConfigServiceimpl implements IBaseConfigService {

    @Autowired
    private BaseConfigMapper baseConfigMapper;
    @Autowired
    private BaseConfig baseConfig;


    @Autowired
    private BaseConfigExtendMapper baseConfigExtendMapper;

    //添加配置类信息
    @Override
    public void saveConfig(BaseConfig baseConfig) throws ServiceException {
        BaseConfig baseConfig1 = baseConfigExtendMapper.findConfigName(baseConfig.getConfigName());//根据配置名获取配置信息
        baseConfig.setConfigStatus(0);
            if (baseConfig1 == null){
                baseConfigMapper.insertSelective(baseConfig);
            }else {
                throw new ServiceException("该配置名字已经存在");
            }

    }

    //根据id更新配置类
    @Override
    public void updateConfig(BaseConfig baseConfig) throws ServiceException {
        
        //如果获取到配置状态值为空或者为零则可以更新
        if(baseConfig.getConfigStatus()==null || baseConfig.getConfigStatus() == 0){
            baseConfigMapper.updateByPrimaryKeySelective(baseConfig);
            return;
        }
        if (baseConfig.getConfigStatus() == 1 && baseConfigExtendMapper.findConfigStaus() != null){

            throw new ServiceException("已有配置类启动，无法更新，请修改status的值");

        }
        baseConfigMapper.updateByPrimaryKeySelective(baseConfig);

    }

    //修改配置状态
    @Override
    public void changeConfigStatus(int id, int status) throws ServiceException {

        BaseConfig baseConfig3 = new BaseConfig();
        BaseConfig baseConfig2 = new BaseConfig();
        baseConfig2 = baseConfigExtendMapper.findConfigStaus();//获取配置类中的状态信息
        //当前配置类的状态不为空，并且id值不等于修改的id值
        if (baseConfig2 != null && baseConfig2.getConfigId() != id){
            throw new ServiceException("无法修改选定配置类的状态,请禁用当前配置类状态");
        }
        baseConfig3.setConfigId(id);
        baseConfig3.setConfigStatus(status);
        baseConfigMapper.updateByPrimaryKeySelective(baseConfig3);


    }


    //根据id删除
    @Override
    public void deleteById(int id) throws ServiceException {

        baseConfig = baseConfigMapper.selectByPrimaryKey(id);
        if (baseConfig.getConfigStatus() == null || baseConfig.getConfigStatus() == 0){
            baseConfigMapper.deleteByPrimaryKey(id);
            return;
        }
        if (baseConfig.getConfigId() == id && baseConfig.getConfigStatus() == 1){//不能通过baseConfig去获取数据库信息，不然会出空指针错误
            throw new ServiceException("请先禁用该配置的状态才能删除");
        }

//         baseConfigMapper.deleteByPrimaryKey(id);

    }

    //分页查询配置类信息
    @Override
    public PageInfo<BaseConfig> findAllWithPage(int pageNum, int pageSize) throws ServiceException {

        PageHelper.startPage(pageNum,pageSize,true);
        List<BaseConfig> list = baseConfigExtendMapper.findAllBaseConfig();
        if (list.size() == 0){
            throw new ServiceException("没有可以查询的配置信息");
        }
        PageInfo<BaseConfig> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //查找当前启用的配置信息
    @Override
    public BaseConfig currentConfigYes() throws ServiceException {
        BaseConfig config = baseConfigExtendMapper.findConfigStaus();
        //查询数据库中配置类实体中是否存在status值为1的结果
        if (config == null){
            throw new ServiceException("没有找到启用的配置信息");
        }
        return config;
    }
}
